﻿#include <fstream>
#include <filesystem>

#include "CodeAnalyzer.h"
#include "FABuilder.h"
#include "ReManager.h"

using namespace std;

/**
 * 从文件路径或者文件内容中获取到数据
 * @param pathOrContent 文件路径或者文件内容
 */
vector<string> initBuffs(const string& pathOrContent);

/**
 * 主程序入口
 */
int main() {
    auto buffs = initBuffs("Path:/to/your/re.txt");
    const auto reManager = ReManager(buffs);

    const auto faBuilder = FABuilder(reManager.reMap, reManager.reOfTokenType, reManager.reForAuxiliary);

    auto codeAnalyzer = CodeAnalyzer(faBuilder.DFAsAndEndsOfTokenType, faBuilder.DFAsAndEndsOfAuxiliary);
    codeAnalyzer.generateCppExe();

    bool isOk = CodeAnalyzer::analyze("Path:/to/your/code.txt");
    if (isOk) cout << "OK" << endl;
    else cout << "FAIL" << endl;
    return 0;
}

vector<string> initBuffs(const string& pathOrContent) {
    // 清空buffs内容
    vector<string> buffs;
    string line;
    if (filesystem::exists(pathOrContent)) {
        // 读取文件内容，存入buffs列表
        if (ifstream f(pathOrContent); f.is_open()) {
            while (getline(f, line)) {
                buffs.emplace_back(line);
            }
            f.close();
        }
        else {
            throw runtime_error("Unable to open file: " + pathOrContent);
        }
    }
    else {
        // 直接是文件内容
        stringstream ss(pathOrContent);
        while (getline(ss, line)) {
            buffs.emplace_back(line);
        }
    }
    return buffs;
}
